package com.api.shuigui.web;


import cn.hutool.core.io.IoUtil;
import com.alibaba.fastjson.JSONObject;

import com.api.shuigui.service.SpecialDocContentService;

import com.api.shuigui.service.impl.SpecialDocContentServiceImpl;

import com.engine.common.util.ParamUtil;
import weaver.general.BaseBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.Year;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 对专题专栏特殊处理
 传入的 只用到了传入的页面大小
 */
@WebServlet("/doc/getSpecialdoc")
public class SpecialDocContentWeb extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        super.doGet(req, res);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json;charset=utf-8");
        //跨域
        /* 允许跨域的主机地址 */
        response.setHeader("Access-Control-Allow-Origin", "*");

        //初始化结果
        Map<String, Object> result = new HashMap<>();
        PrintWriter out = null;
        try {
            out = response.getWriter();
            //读取流中的参数
            String paramJson = IoUtil.read(request.getInputStream(), "UTF-8");
            //初始化参数
            Map<String, Object> params;
            if ("".equals(paramJson)) {
                params = ParamUtil.request2Map(request);
            } else {
                params = JSONObject.parseObject(paramJson, Map.class);
            }




            //获取数据
            SpecialDocContentService doc = new SpecialDocContentServiceImpl();
            Map<String, Object> docContent = doc.getDocContent(params);

            result.putAll(docContent);

            result.put("status", 200);
            //返回数据
            out.write(JSONObject.toJSONString(result));
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
            new BaseBean().writeLog("get_data_error:"+e);
            result.put("status", -1);
            result.put("msg", "获取数据异常！");
            if (out != null) {
                out.write(JSONObject.toJSONString(result));
                out.flush();
            }
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
}
